from sqlalchemy.orm import Session
from app.core.responses import Response
from app.database import engine
from app.models import Ad, AdSlot
from app.schemas.ad import AdSM, AdSlotSM


class AdServices:

    @staticmethod
    def store(form: AdSM):
        with Session(engine) as session:
            points = Ad(**form.dict())
            session.add(points)
            session.commit()
        return True

    @staticmethod
    def delete(id: int):
        with Session(engine) as session:
            session.query(Ad).filter(Ad.id == id).delete()
            session.commit()
        return True

    @staticmethod
    def update(id: int, form: AdSM):
        with Session(engine) as session:
            music = session.query(Ad).filter_by(id=id)
            music.update(form.dict())
            session.commit()
        return True

    @staticmethod
    def slot_index(search: str = ''):
        with Session(engine) as session:
            ad_list = session.query(AdSlot).filter_by(name=search).all()
            return Response.success(data=ad_list)

    @staticmethod
    def slot_store(form: AdSlotSM):
        with Session(engine) as session:
            points = AdSlot(**form.dict())
            session.add(points)
            session.commit()
        return True

    @staticmethod
    def slot_delete(id: int):
        with Session(engine) as session:
            session.query(AdSlot).filter(AdSlot.id == id).delete()
            session.commit()
        return True

    @staticmethod
    def slot_update(id: int, form: AdSlotSM):
        with Session(engine) as session:
            music = session.query(AdSlot).filter_by(id=id)
            music.update(form.dict())
            session.commit()
        return True
